#include "worker.h"

#include <QFile>
#include <QIODevice>

Worker::Worker(QObject *parent) : QObject(parent)
{

}

void Worker::copyFile(const QString& srcFile, const QString& dstFile)
{
    QFile f1(srcFile), f2(dstFile);

    f1.open(QIODevice::ReadOnly);
    f2.open(QIODevice::WriteOnly | QIODevice::Truncate);

    char buff[4 * 1024];
    qint64 ret, copyedCnt = 0, fileSize = f1.size(), prevProgress = 0, curProgress = 0;

    while((ret = f1.read(buff, sizeof(buff))) > 0)
    {
        f2.write(buff, ret);

        copyedCnt += ret;

        curProgress = 100.0 * copyedCnt / fileSize;

        if(curProgress > prevProgress)
        {
            emit updateCopyProgress(curProgress);
            prevProgress = curProgress;
        }
    }
    emit copyFinished();
}
